<?php
/**
 * 首页控制器
 * @author jason <yangjs17@yeah.net> 
 * @version TS3.0
 */
class ResourcesAction extends Action {
	
	/**
	 * 我的首页
	 *
	 * @return void
	 */
	public function index() {
		$type=$_GET["type"];
		$data=D("resource_type")->where("parent=0 and type=1")->select();
		foreach ($data as $key => $value) {
			$data[$key]["level"]=D("resource_type")->where("type=1 and parent=".$value["id"])->field("id,name")->select();
			$resource_type=getSubByKey($data[$key]["level"],"id");
			if($type>0){
				$data[$key]["data"]=D("resource")->where(array("is_del"=>0,"resource_type"=>$type))->select();
			}else{
				$data[$key]["data"]=D("resource")->where(array("is_del"=>0,"type"=>1))->select();
			}
			foreach ($data[$key]["data"] as $k => $v) {
				$data[$key]["data"][$k]["img_url"]=$resource_data[$ke]["img_url"]=getAttachUrlByAttachId($v["resource_img"]);
			}
		}
		
		$this->assign("data",$data);
		$this->display();
	}
	//学习资源
	public function TeacherResources() {
		$data=D("resource_type")->where("parent=0 and type=2")->select();
		foreach ($data as $key => $value) {
			$data[$key]["level"]=D("resource_type")->where("type=2 and parent=".$value["id"])->field("id,name")->select();
			foreach ($data[$key]["level"] as $k => $v) {
				if($v["id"]==31 || $v["id"]==46){
					$resource=M("resource")->group("resource_teacher")->where("is_del=0")->order("count(id)")->field("resource_teacher")->limit(10)->select();
					$resource_teacher=getSubByKey($resource,"resource_teacher");
					$data[$key]["level"][$k]["level"]=D("resource_teacher")->where(array("id"=>array('in',$resource_teacher)))->field("id,name")->select();
				}else{
					$data[$key]["level"][$k]["level"]=D("resource_type")->where("type=2 and parent=".$v["id"])->field("id,name")->select();
				}
			}
			$resource_type=getSubByKey($data[$key]["level"],"id");
			$resource_data=D("resource")->where(array("type"=>2,"is_del"=>0,"resource_type"=>array("in",$value["id"])))->limit(4)->select();
			foreach ($resource_data as $ke => $va) {
    			$resource_data[$ke]["img_url"]=getAttachUrlByAttachId($va["resource_img"]);
			}
			$data[$key]["data"]=$resource_data;
		}
		$this->assign("data",$data);
		$this->display();
	}
	public function view(){
		$id=$_GET["id"];
    	$data=M("resource")->where("is_del=0 and id=".$id)->find();
    	$data["img_url"]=getAttachUrlByAttachId($data["resource_img"]);
    	$data["file_url"]=getAttachUrlByAttachId($data["attach_id"]);
        $resource_id=explode("/",$data["resource_type"]);
    	$resource_type=M("resource_type")->where("id=".$resource_id[0])->find();
    	$data["type_name"]=$resource_type["name"];
    	$resource_type=M("resource_type")->findALL();
        $tj_data=M("resource")->where("is_tj=1 and is_del=0 and id!=".$id)->order("ctime desc")->field("id,resource_img,resource_name")->limit("4")->select();
        foreach ($tj_data as $key => $value) {
            $tj_data[$key]["img_url"]=getAttachUrlByAttachId($value["resource_img"]);
        }
		$this->assign("tj_data",$tj_data);
        $this->assign("resource_type",$resource_type);
    	$this->assign("data",$data);
    	$this->display();
	}
	public function more(){
		$level=D("resource_type")->where("type=2 and parent=".intval($_GET["id"]))->field("id,name")->select();
		foreach ($level as $k => $v) {
			if($v["id"]==31 || $v["id"]==46){
				$resource=M("resource")->where("is_del=0")->group("resource_teacher")->order("count(id)")->field("resource_teacher")->limit(10)->select();
				$resource_teacher=getSubByKey($resource,"resource_teacher");
				$data[$key]["level"][$k]["level"]=D("resource_teacher")->where(array("id"=>array('in',$resource_teacher)))->field("id,name")->select();
			}else{
				$level[$k]["level"]=D("resource_type")->where("type=2 and parent=".$v["id"])->field("id,name")->select();
			}
		}
		$this->assign("level",$level);
		$id=$_GET["id"];
        $category=$_GET["category"];
        $resource_type=$_GET["resource_type"];
        $resource_teacher=$_GET["resource_teacher"];
        $map["is_del"]=0;
        $map["resource_type"]=intval($_GET["id"]);
        if($category){
            $map["category"]=$category;
        }
        if($resource_type){
            $map["resource_type"]=$resource_type;
        }
        if($resource_teacher){
            $map["resource_teacher"]=$resource_teacher;
        }
        if(!$map){
        	$resource_type=M("resource_type")->where("parent=".$id)->field("id,name")->select();
			$types=getSubByKey($resource_type,"id");
			$map["resource_type"]=array("like","'%".$types."%'");
        }
    	$data=M("resource")->where($map)->findPage(12);
    	foreach ($data['data'] as $key => $value) {
    		$data['data'][$key]["img_url"]=getAttachUrlByAttachId($value["resource_img"]);
    	}
		$type=M("resource_type")->where("id=".$id)->field("name")->find();
		$this->assign("type_name",$type["name"]);
    	$this->assign("data",$data);
    	$this->assign("id",$id);
    	$this->display();
	}
	function getResourceList(){
        $category=$_POST["category"];
        $resource_type=$_POST["resource_type"];
        $resource_teacher=$_POST["resource_teacher"];
        $map["is_del"]=0;
        if($category){
            $map["category"]=$category;
        }
        if($resource_type){
            $map["resource_type"]=$resource_type;
        }
        if($resource_teacher){
            $map["resource_teacher"]=$resource_teacher;
        }
        $data= M("resource")->where($map)->limit(4)->select();
        foreach ($data as $key => $value) {
        	$data[$key]["img_url"]=getAttachUrlByAttachId($value["resource_img"]);
        }
        if ($data) {
            $this->assign('listData', $data);
            $this->assign('where', $where);
            $html = $this->fetch('resource_list');
        } else {
            $html = $this->fetch('resource_list');
        }
        $data = $html;
        echo $data;
        exit();
	}
}	           